
<script setup>
import { ref , onMounted} from 'vue';
import { getSubCategoryAPI , getCategoryGoodsAPI} from '@/apis/category.js'
import { useRoute } from 'vue-router';
import GoodsItem from '@/components/GoodsItem.vue';

// 页面路由对象
const route  = useRoute()

// 二级分类数据
const subCategory = ref({})

// 获取二级分类数据
const getSecondCategory = async () => {
    const res = await getSubCategoryAPI(route.params.id)
    subCategory.value = res.result
    // console.log(res);
}

// 商品列表请求参数
const goodsParams = ref({
    categoryId: 1005000 ,
     page: 1,
     pageSize: 20,
     sortField: 'publishTime'
})

// 商品数据
const goodsList = ref([])

// 获取商品列表
const getGoodsList = async () => {
    const res = await getCategoryGoodsAPI(goodsParams.value)
    console.log(res);
    goodsList.value = goodsList.value.concat(res.result.items)

    // 终止加载
    if (res.result.items.length === 0) {
        scrollDisabled.value = true
    }
}


// 无限加载
const scrollDisabled = ref(false)
// tab切换
const tabChange = () => {
  goodsParams.value.page = 1
  getGoodsList()
  // console.log(goodsParams.value.sortField);
}

// 初始化调用
onMounted(() => {
    getSecondCategory()
    getGoodsList()
})

// 触底加载
const loadingGoodList = () => {
    goodsParams.value.page++
    getGoodsList()
    console.log('cscsc');
}


</script>

<template>
  <div class="container ">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/category/${subCategory.parentId
}` }">{{ subCategory.parentName }}
        </el-breadcrumb-item>
        <el-breadcrumb-item>居家生活用品</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs v-model="goodsParams.sortField" @tab-change="tabChange" >
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div class="body" v-infinite-scroll="loadingGoodList" :infinite-scroll-disabled="scrollDisabled">
         <!-- 商品列表-->
         <GoodsItem v-for="item in goodsList" :key="item.id" :goods="item"></GoodsItem>
      </div>
    </div>
  </div>

</template>



<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
    justify-content: center;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 10px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }


}
</style>